<template>
  <el-container>
    <el-header><h1>用户信息编辑</h1></el-header>
    <el-main>
      <el-form label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="user.nickname"></el-input>
        </el-form-item>
        <el-form-item label="实验室主任">
          <el-input v-model="user.lab_director"></el-input>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="user.contact"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">保存</el-button>
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>

<script>
import { reactive, onMounted } from 'vue';

export default {
  setup() {
    const user = reactive({
      username: '',
      nickname: '',
      lab_director: '',
      contact: ''
    });

    const save = () => {
      // 在这里实现保存用户信息的逻辑
      console.log('保存用户信息', user);
      // 省略保存逻辑
      alert('用户信息已保存');
    };

    // 模拟异步获取用户信息的过程，这里使用 setTimeout
    const getUserInfo = () => {
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve({
            username: 'John Doe',
            nickname: 'JD',
            lab_director: 'Dr. Smith',
            contact: 'john.doe@example.com'
          });
        }, 1000);
      });
    };

    onMounted(async () => {
      // 在页面加载完成后，通过异步请求获取用户信息并赋值给 user 对象
      const userInfo = await getUserInfo();
      user.username = userInfo.username;
      user.nickname = userInfo.nickname;
      user.lab_director = userInfo.lab_director;
      user.contact = userInfo.contact;
    });

    return {
      user,
      save
    };
  }
}
</script>
